package org.contacts.dao.impl;

import org.contacts.dao.Contact;
import org.contacts.dao.IContactDAO;
import org.contacts.dao.IDataAccessObject;
import org.contacts.dao.impl.ContactDAOFactory;
import org.contacts.dao.util.ConnectionFactory;

import java.sql.*;
import java.util.List;

/**
 * Created by nnazarov on 3/27/14.
 */
public class ContactDAO implements IDataAccessObject {

    private int id_;
    private String fname_;
    private String lname_;
    private String phone_;



    private Connection connection;
    private Statement statement;
    private ResultSet resultSet;
    private String query;

    public ContactDAO(){
        try {
            connection = ConnectionFactory.getConnecion();
            statement = connection.createStatement();
        }
        catch (SQLException se) {
            se.printStackTrace();
        }
    }

//    @Override
    public String getContactsAsString() {

        String contacts = ""; // tab separated format
        query = "SELECT * from contact";

        try {
            if (statement.execute(query)) {
                resultSet = statement.getResultSet();
                while (resultSet.next()) {
                    contacts += resultSet.getString(2) + "\t"; // fname
                    contacts += resultSet.getString(3) + "\t"; // lname
                    contacts += resultSet.getString(4) + "\n"; // phone
                }
            }
            else {
                System.out.println("Selection failed");
            }
        }
        catch (SQLException se) {
            System.out.println("Something is wrong with statement or query");
        }

        return contacts;
    }
/*
    @Override
    public List<Contact> getAllContacts() {
        return null;
    }

    @Override
    public boolean deleteContact(String phone) {
        query = "delete from contact\n" +
                "where phone=" + phone;

        try {
            if (statement.execute(query)) {
                return true;
            }
            else {
                System.out.println("Contact not deleted");
                return false;
            }
        }
        catch (SQLException se) {
            System.out.println("Something is wrong with statement or query");
        }
        return false;
    }

    @Override
    public boolean insertContact(Contact contact) {
        return false;
    }

    @Override
    public boolean updateContact(String phone) {
        return false;
    }

    @Override
    public Contact findContact(String phone) {
        query = "select fname, lname, phone " +
                "from contact \n" +
                "where phone=\"" + phone + "\"";

        Contact result;
        try {
            if (statement.execute(query)) {
                resultSet = statement.getResultSet();
                System.out.println(resultSet.getString(1));
            }
            else {
                System.out.println("Contact not found");

            }
        }
        catch (SQLException se) {
            System.out.println("Something is wrong with statement or query");
        }
        return null;
    }*/


    public int getId_() {
        return id_;
    }

    public void setId_(int id_) {
        this.id_ = id_;
    }

    public String getFname_() {
        return fname_;
    }

    public void setFname_(String fname_) {
        this.fname_ = fname_;
    }

    public String getLname_() {
        return lname_;
    }

    public void setLname_(String lname_) {
        this.lname_ = lname_;
    }

    public String getPhone_() {
        return phone_;
    }

    public void setPhone_(String phone_) {
        this.phone_ = phone_;
    }
}
